package com.seeyon.apps.commonExt.util;

import com.seeyon.cap4.form.bean.*;
import com.seeyon.cap4.form.exception.FormException;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

import java.util.Optional;


/**
 * Description
 * <pre></pre>
 * @author FanGaowei<br>
 * Copyright(c) Beijing Seeyon Software Co.,LTD
 */
public class CAP4FormUtilExt {

	public static Optional<Object> getFieldValue(FormDataBean bean, String disPlay) {
        return Optional.ofNullable(bean).map(FormDataBean::getFormTable).map(o -> o.getFieldBeanByDisplay(disPlay));
	}


	public static <T> Optional<T> getFieldValue(FormDataBean bean, String disPlay, Class<T> cls) {
        return Optional.ofNullable(bean)
                .map(FormDataBean::getFormTable).map(o -> o.getFieldBeanByDisplay(disPlay))
                .map(field -> Convert.convert(cls, bean.getFieldValue(field.getName())));
	}

    public static <T> T  getFieldValueThrow(FormDataBean bean, String disPlay, Class<T> cls) {
        Assert.notNull(bean, "表单为空！");
        return Optional.ofNullable(bean)
                .map(FormDataBean::getFormTable).map(o -> o.getFieldBeanByDisplay(disPlay))
                .map(field -> Convert.convert(cls, bean.getFieldValue(field.getName())))
                .orElseThrow(() -> new FormException(StrUtil.format("表单【{}】没有定义【{}】字段！", bean.getFormTable().getTableName(), disPlay)));
    }

    public static <T> T getFieldValue(FormDataBean bean, String disPlay, Class<T> cls, T defaultValue) {
        if(bean == null) {
            return defaultValue;
        }
        FormTableBean table = bean.getFormTable();
        if(table == null) {
            return defaultValue;
        }
        FormFieldBean field = table.getFieldBeanByDisplay(disPlay);
        if(field == null) {
            return defaultValue;
        }
        return Convert.convert(cls, bean.getFieldValue(field.getName()), defaultValue);
    }


	public static String getFieldName(FormTableBean formTableBean, String disPlay) {
        Assert.notNull(formTableBean, "表单为空！");
        return Optional.ofNullable(formTableBean)
                .map(o -> o.getFieldBeanByDisplay(disPlay))
                .map(FormFieldBean::getName)
                .orElseThrow(() -> new FormException(StrUtil.format("表单【{}】，没有定义【{}】字段！", formTableBean.getTableName(), disPlay)));
	}

    public static FormFieldBean getFieldBean(FormTableBean formTableBean, String disPlay) {
        Assert.notNull(formTableBean, "表单为空！");
        return Optional.ofNullable(formTableBean)
                .map(o -> o.getFieldBeanByDisplay(disPlay))
                .orElseThrow(() -> new FormException(StrUtil.format("表单【{}】，没有定义【{}】字段！", formTableBean.getTableName(), disPlay)));
    }
	

	
	/**
	 * Description:
	 * <pre></pre>
	 * @param bean 这里的bean必须是getMasterBean() 方法获取到的bean
	 * @param disPlay 
	 * @param value
	 */
	public static void setCellValue(FormDataBean bean, String disPlay, Object value) {
        Assert.notNull(bean, "数据为空！");
		FormFieldBean cell = getFieldBean(bean.getFormTable(), disPlay);
		if(cell != null) {
			bean.addFieldValue(cell.getName(), value);
		}
	}
	
	public static void setCellValue(FormDataSubBean bean, String disPlay, Object value) {
        Assert.notNull(bean, "数据为空！");
        FormFieldBean cell = getFieldBean(bean.getFormTable(), disPlay);
        if(cell != null) {
            bean.addFieldValue(cell.getName(), value);
        }
    }
	

	/**
	 * 根据子表的名称获取子表的tableBean
	 * @param formBean
	 * @param disPlay
	 * @return
	 */
	public static FormTableBean getSubTableBeanByDisplayThrow  (FormBean formBean, String disPlay) {
		return formBean.getSubTableBean().stream()
				.filter(item -> disPlay.equals(item.getDisplay()))
				.findFirst()
				.orElseThrow(() -> new FormException(String.format("表单【%s】，不存在【%s】子表单", formBean.getMasterTableBean().getDisplay(), disPlay)));
	}
}
